package com.example.localbites;

import com.example.constants.StringConstant;
import com.example.dbaccess.LocalBitesDB;
import com.example.yelp.service.beans.Business;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class RestaurantDetailsActivity extends Activity {

	private Business resultData = null;
	private StringConstant con = new StringConstant();
	private MapView mapV;
	MapController mControl;
	GeoPoint GeoP;
	//private Button addToFav;
	private LocalBitesDB localBitesDB;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.restaurant_details);
		
		resultData = (Business) getIntent().getSerializableExtra(con.RESTAURANT_DETAILS);
		TextView restaurantName = (TextView) findViewById(R.id.txtRestaurantName);
		restaurantName.setText(resultData.getName());
		
		TextView restaurantAddress = (TextView) findViewById(R.id.txtRestaurantAddress);
		restaurantAddress.setText(resultData.getDisplayAddress());
		
		TextView contactInfo = (TextView) findViewById(R.id.txtContactInformation);
		contactInfo.setText(resultData.getDisplayPhone());
		
		TextView restaurantRating = (TextView) findViewById(R.id.txtRestaurantRating);
		restaurantRating.setText(Double.toString(resultData.getRating()));
		
		//addToFav  = (Button) findViewById(R.id.btnFavorite);
		//Displaying Map Component
		/*mapV = (MapView) findViewById(R.id.mapView);
		mapV.displayZoomControls(true);
		mapV.setBuiltInZoomControls(true);
		
		double lat = resultData.getLocation().getCoordinate().getLatitude();
		double lng = resultData.getLocation().getCoordinate().getLongitude();
		
		GeoP = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
		
		mControl = mapV.getController();
		mControl.animateTo(GeoP);
		mControl.setZoom(13);*/
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.restaurant_details, menu);
		return true;
	}
	public void onCallClick(View view)
	{
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri.parse("tel:" + resultData.getPhone()));
		startActivity(callIntent);

	}
	public void onViewMap(View view)
	{
		Intent viewIntent = new Intent(this, Display_Map.class);
		
		if(resultData != null)
		{
			viewIntent.putExtra(con.LATITUDE, resultData.getLattitude());
			viewIntent.putExtra(con.LONGITUDE, resultData.getLongitude());
			startActivity(viewIntent);
		}
		
	}
	public void onAddToFavorite(View view)
	{
		boolean didItWork = true;
		try
		{
			localBitesDB = new LocalBitesDB(RestaurantDetailsActivity.this);
			//Opening the Database
			localBitesDB.open();
			
			//Writing to Database
			localBitesDB.addData(resultData.getName(),
						resultData.getDisplayAddress(),
						resultData.getDisplayPhone(),
						resultData.getRating(),
						resultData.getDistance(),
						resultData.getLattitude(),
						resultData.getLongitude());
			
			//Closing the Database
			localBitesDB.close();
		}
		catch(Exception e)
		{
			didItWork = false;
			
		}
		finally
		{
			/*if(didItWork)
			{
				Dialog d = new Dialog(this);
				d.setTitle("SQL WORKING");
				TextView v = new TextView(this);
				v.setText("Success");
				d.setContentView(v);
				d.show();
			}*/
			
		}
	}

}
